//
//  ECUITextField_Search.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/1/27.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECUITextField_Search.h"
@interface ECUITextField_Search()
@property(nonatomic,strong)UIButton *clearButton;
@end
@implementation ECUITextField_Search
@synthesize textField = _textField;
@synthesize searchButton = _searchButton;
- (void)addSubviews
{
    [super addSubviews];
    [self addSubview:self.textField];
    [self addSubview:self.searchButton];
    self.searchButton.frame = CGRectMake(self.textField.width, CGRectGetMinY(self.textField.frame), CGRectGetWidth(self.bounds)-CGRectGetWidth(self.textField.frame), CGRectGetHeight(self.textField.frame));
}
- (UITextField *)textField
{
    if (!_textField) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, kProportion_Width(780), 35)];
        _textField.placeholder = @"搜索课程";
        _textField.autocorrectionType = UITextAutocorrectionTypeNo;
        _textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _textField.clearButtonMode = UITextFieldViewModeNever;
        _textField.delegate = self;
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _textField.height, _textField.height)];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CourseList_search_image"]];
        imageView.frame = CGRectMake(10, 10, 15, 15);
        [leftView addSubview:imageView];
        _textField.tintColor = [UIColor colorWithHex:0x333333];
        _textField.leftView = leftView;
        _textField.backgroundColor = [UIColor colorWithHex:0xf0f0f0];
        _textField.textColor = [UIColor colorWithHex:0x333333];
        _textField.font = [UIFont systemFontOfSize:15];
        _textField.returnKeyType = UIReturnKeySearch;
        _textField.leftViewMode = UITextFieldViewModeAlways;
        
        _textField.rightView = self.clearButton;
        _textField.rightViewMode = UITextFieldViewModeWhileEditing;
    }
    return _textField;
}
- (UIButton *)clearButton
{
    if (!_clearButton) {
        _clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _clearButton.frame = CGRectMake(0, 0, 40, 30);
        _clearButton.backgroundColor = [UIColor clearColor];
        [_clearButton setImage:[UIImage imageNamed:@"CourseList_search_clearRightImage"] forState:UIControlStateNormal];
        [_clearButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
        [_clearButton addTarget:self action:@selector(action_clear:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearButton;
}
- (UIButton *)searchButton
{
    if (!_searchButton) {
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _searchButton.backgroundColor = [UIColor clearColor];
        _searchButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [_searchButton setTitle:@"搜索" forState:UIControlStateNormal];
        [_searchButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
        [_searchButton setTitleColor:[UIColor colorWithHex:0xff6700] forState:UIControlStateNormal];
        _searchButton.showsTouchWhenHighlighted = YES;
        [_searchButton addTarget:self action:@selector(action_search:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchButton;
}
-(void)action_search:(id)sender{
    //    关闭键盘
    [self.textField resignFirstResponder];
    if (self.block_search) {
        self.block_search(self.textField.text);
    }
    
}
-(void)action_clear:(id)sender{
    self.textField.text = @"";
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.textField resignFirstResponder];
    if (self.block_search) {
        self.block_search(self.textField.text);
    }
    return YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
